package com.wedo.mall.activity.user;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.e.library.activity.EActivity;
import com.e.library.http.EApi;
import com.e.library.http.EObserver;
import com.e.library.http.EResponse;
import com.e.library.listener.EListener;
import com.e.library.utils.EUtils;
import com.e.library.utils.EViewUtils;
import com.e.library.widget.ETitleBar;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.wedo.mall.R;
import com.wedo.mall.adapter.MyAddressAdapter;
import com.wedo.mall.entity.MyAddress;
import com.wedo.mall.ptr.Pull2Refresh;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MyAddressActivity extends EActivity {

    public static final String EXTRA_ADDRESS = "address";
    Boolean firstOpen = true;

    @BindView(R.id.title_bar) ETitleBar title_bar;
    @BindView(R.id.tv_empty) TextView tv_empty;
    @BindView(R.id.rv_list) XRecyclerView rv_list;

    private MyAddressAdapter myAddressAdapter;

    private Pull2Refresh<MyAddress> ptr;

    @Override
    protected int layout() {
        return R.layout.activity_my_address;
    }

    @Override
    protected void onActivityCreated(Bundle state) {
        title_bar.setForwardText(getResources().getString(R.string.user_createaddress));
        title_bar.showForwordTv();
        title_bar.setForwardTextColor(getResources().getColor(R.color.user_addresslist_font));

        rv_list.setHasFixedSize(true);
        EViewUtils.vertical(rv_list);

        myAddressAdapter = new MyAddressAdapter(this,null);
        ptr = new Pull2Refresh<>();

        EObserver observer = new EObserver() {
            @Override
            public void successful(EResponse response) {
                List<MyAddress> list = response.list(MyAddress.class);
                if (!EUtils.isEmpty(list)){
                    Collections.sort(list,new Sort());
                }
                ptr.finish(list);
            }

            @Override
            public void failure(String msg) {
                ptr.finish(null);
            }
        };

        ptr.widget(rv_list).adapter(myAddressAdapter).url(EApi.USER_AllCONSIGNEE).clazz(MyAddress.class).emptyWidget(tv_empty).observer(observer).done();

        int endpoint = getIntent().getIntExtra("endpoint",0);
        if (endpoint == 1){
            return;
        }
        myAddressAdapter.setItemClickListener(new EListener<MyAddress>() {
            @Override
            public void onInvoked(View view, MyAddress data, int position) {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putParcelable(EXTRA_ADDRESS,data);
                intent.putExtras(bundle);
                setResult(RESULT_OK,intent);
                defaultFinish();
            }
        });
    }

    public void refresh(){
        ptr.refresh();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(firstOpen){
            firstOpen = false;
        }else{
            ptr.refresh();
        }

    }

    @OnClick(R.id.tv_forward)
    protected void onCreateAddressClick(){
        toActivity(CreateAddressActivity.class);
    }

    /**排序，默认地址显示在第一位*/
    private class Sort implements Comparator<MyAddress>{

        @Override
        public int compare(MyAddress o1, MyAddress o2) {
            if (TextUtils.equals("1",o1.is_default)){
                return 1;
            }
            if (TextUtils.equals("1",o2.is_default)){
                return 1;
            }
            return 0;
        }
    }
}
